package com.yunbao.ioc.tx;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by louisyuu on 2020/3/4 下午2:42
 */
public class ConnectionHolder {


    private final static ThreadLocal<Connection> connectionHolder = new ThreadLocal<>();


    private DataSource dataSource;


    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public Connection getCurrentThreadConnection() {
        try {
            Connection connection = connectionHolder.get();
            if (connection == null) {
                connectionHolder.set(dataSource.getConnection());
            }
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return null;

    }


}
